package com.xlinur.android.moviecatalog;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
//TODO создание файла
public class FileBrowser extends ListActivity {
	private List<String> mEntries = new ArrayList<String>();
	private File mCurrent = new File(File.separator + "sdcard");

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		browseToRoot();
	}

	@Override
	public void onStart() {
		super.onStart();
		String filename = this.getIntent().getStringExtra("file");
		if (filename != null && !filename.equals("")) {
			File file = new File(filename);
			if (!file.isDirectory()) {
				file = file.getParentFile();
			}
			browseTo(file);
		} else {
			browseToRoot();
		}
	}

	private void browseToRoot() {
		browseTo(new File(File.separator + "sdcard"));
	}

	private void upOneLevel() {
		if (mCurrent.getParent() != null)
			browseTo(mCurrent.getParentFile());
	}

	private void browseTo(final File aDirectory) {
		this.setTitle(aDirectory.getAbsolutePath() + " :: "
				+ getString(R.string.app_name));
		if (aDirectory.isDirectory()) {
			mCurrent = aDirectory;
			fill(aDirectory.listFiles());
		} else {
			Intent returnIntent = new Intent();
			returnIntent.putExtra("file", aDirectory.getPath());
			setResult(RESULT_OK, returnIntent);
			finish();
		}
	}

	private void fill(File[] files) {
		mEntries.clear();
		List<String> dirs = new ArrayList<String>();
		List<String> fils = new ArrayList<String>();
		mEntries.add(getString(R.string.current_dir));
		if (mCurrent.getParent() != null)
			mEntries.add(getString(R.string.up_one_level));
		if (files != null) {
			for (File currentFile : files) {
				int currentPathStringLenght = mCurrent.getAbsolutePath()
						.length();
				if (currentFile.isDirectory()) {
					String dir = currentFile.getAbsolutePath().substring(
							currentPathStringLenght);
					if (!dir.startsWith(File.separator)) {
						dir = File.separator + dir;
					}
					dirs.add(dir);
				} else {
					fils.add(currentFile.getAbsolutePath().substring(
							currentPathStringLenght + 1));
				}
			}
		}
		Collections.sort(dirs);
		Collections.sort(fils);
		mEntries.addAll(dirs);
		mEntries.addAll(fils);
		this.setListAdapter(new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_1, mEntries));
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);
		String selectedFileString = mEntries.get(position);
		if (selectedFileString.equals(getString(R.string.current_dir))) {
			browseTo(mCurrent);
		} else if (selectedFileString.equals(getString(R.string.up_one_level))) {
			this.upOneLevel();
		} else {
			File clickedFile = null;
			String entry = mEntries.get(position);
			if (!entry.startsWith(File.separator)) {
				entry = File.separator + entry;
			}
			clickedFile = new File(mCurrent.getAbsolutePath() + entry);
			if (clickedFile != null)
				this.browseTo(clickedFile);
		}
	}
}